﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;
using Newtonsoft.Json.Linq;
using System.Data;

namespace AdminUI.Pages.BaseData
{
    public partial class AlbumModel : BaseModel
    {
        public void OnGet(int rid = 0)
        {
            if (rid == 0) rid = OnlineTID;//没指定rid值的都是关联租户的

            RelationGoodsID = rid;
            var req = new PagingRequest
            {
                PageIndex = 1,
                PageSize = 20
            };

            var result = AlbumBO.Instance.Query(req, OnlineTID, RelationGoodsID); 
            ViewBag.DataSource = result.Data;
            ViewBag.Total = result.Total;
            ViewBag.PageSize = result.PageSize;

        }

        /// <summary>
        /// 
        /// </summary>
        public int RelationGoodsID { get; set; }

        /// <summary>
        /// 执行会引起表格控件数据变化的相关操作
        /// </summary>
        /// <param name="searchCondition">搜索条件</param>
        /// <param name="searchText">搜索关键字文本</param>
        /// <param name="selectedIDs">已选记录ID数组</param>
        /// <param name="Grid1_fields">当前表格控件所有列名称数组（注意：事件触发控件需要指定表格所在容器ID）</param>
        /// <returns></returns>
        public IActionResult OnPostGrid_DoAction(QueryData<int> data, string condition, string keyword, int goodsID)
        {
            var grid = UIHelper.Grid("Grid1");
            var req = new PagingRequest
            {
                PageIndex = data.PageIndex + 1, //注意：数据表格组件分页索引从0开始
                PageSize = data.ChangedPageSize,
                SortExp = data.SortExp,
            };

            if (!string.IsNullOrEmpty(keyword) && !string.IsNullOrEmpty(condition))
            {
                req.FilterItems.Add(new FilterItem { Field = condition, Value = keyword });
            }

            switch (data.ActionType)
            {
                case "delete":
                    AlbumBO.Instance.Delete(data.SelectedID);
                    LogAction(ActionType.删除, $"删除相册图片：{data.SelectedID}");
                    break;
                case "save":
                    {
                        foreach (var row in data.ModifiedData)
                        {
                            var status = row.Value<string>("status"); //行数据状态
                            var id = Convert.ToInt32(row.Value<int>("id")); //行数据主键ID值
                            var dic = row.Value<JObject>("values").ToObject<Dictionary<string, object>>(); //行数据字典

                            if (status == "modified")
                            {
                                AlbumBO.Instance.ModifyByDic(id, dic);
                            }
                        }
                    }
                    break;
                default:
                    break;
            }

            if (data.IsChangedPageSize)
            {
                grid.PageSize(data.ChangedPageSize);
                grid.PageIndex(0); //改变PageSize大小后重置分页索引，表格控件页索引从0开始
                req.PageIndex = 1;
            }

            var result = AlbumBO.Instance.Query(req, OnlineTID, goodsID);
            grid.RecordCount(result.Total);
            grid.DataSource(result.Data, data.Fields);

            return UIHelper.Result();
        }

        public IActionResult OnPostGrid1_SaveEdit(string[] Grid1_fields, JArray Grid1_modifiedData)
        {
            foreach (JObject row in Grid1_modifiedData)
            {
                var status = row.Value<string>("status"); //行数据状态
                var id = Convert.ToInt32(row.Value<int>("id")); //行数据主键ID值
                var dic = row.Value<JObject>("values").ToObject<Dictionary<string, object>>(); //行数据字典

                if (status == "modified")
                {
                    AlbumBO.Instance.ModifyByDic(id, dic);
                }
            }

            //UIHelper.Grid("Grid1").DataSource(source, Grid1_fields);
            //UIHelper.Label("labResult").Text(String.Format("用户修改的数据：<pre>{0}</pre>", Grid1_modifiedData.ToString(Newtonsoft.Json.Formatting.Indented)), false);


            //HttpContext.Session.SetObject<DataTable>(KEY_FOR_DATASOURCE_SESSION, source);

            ShowNotify("数据保存成功！");

            return UIHelper.Result();
        }
    }
}
